/**
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 

示例：

输入：1->2->4, 1->3->4
输出：1->1->2->3->4->4

 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
// var mergeTwoLists = function(l1, l2) {
//     if(l1 == null) {
//         return l2;
//     }
//     if(l2 == null) {
//         return l1;
//     }
//     if(l1.val > l2.val) {
//         l2.next = mergeTwoLists(l2.next, l1);
//         return l2;
//     } else {
//         l1.next = mergeTwoLists(l1.next, l2);
//         return l1;
//     }
// };
var mergeTwoLists = (l1, l2) => {
    if(l1 == null && l2 == null) {
        return null;
    }
    let head = {val: 0, next: null}
    let currentNode = head;
    while(l1 != null && l2 != null) {
        if(l1.val > l2.val) {
            currentNode.next = l2;
            l2 = l2.next;
        } else {
            currentNode.next = l1;
            l1 = l1.next;
        }
        currentNode = currentNode.next;
    }
    if(l1 != null) {
        currentNode.next = l1;
    } else {
        currentNode.next = l2;
    }
    return head.next;
}
function run () {
    let args = [
        {
            val: 1,
            next: {
                val: 2,
                next: {
                    val: 4,
                    next: null
                }
            }
        },
        {
            val: 1,
            next: {
                val: 3,
                next: {
                    val: 4,
                    next: {
                        val: 5,
                        next: null
                    }
                }
            }
        }
    ];
    return mergeTwoLists(...args);
}
module.exports = run;